﻿using Acme.SmartWMS.RBAC;
using Acme.SmartWMS.RBAC.Roles;
using Acme.SmartWMS.RBAC.Roles.DTO.UpdateDto;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace Acme.SmartWMS.Web.Controllers.RBAC
{
    public class RoleController : Controller
    {
        private readonly IRoleAppService _roleManager;

        public RoleController(IRoleAppService roleManager)
        {
            _roleManager = roleManager;
        }
        /// <summary>
        /// 首页显示
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///  添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddView()
        {
            return View();
        }

        /// <summary>
        ///  添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateView(int id)
        {
            ViewBag.id = id;
            return View();
        }


        /// <summary>
        /// 用户显示（分页）
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="limit">页容量</param>
        /// <param name="accountName">用户名</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> Display(int page, int limit, string? roleName)
        {
            var accountlist = await _roleManager.Display(page, limit, roleName);
            return Json(new { data = accountlist, code = 0 });

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<int> Delete(int id)
        {
            var accountDelete = await _roleManager.Delete(id);
            return accountDelete;
        }


        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public async Task<Role> Backfill(int id)
        {
            var accountList = await _roleManager.Backfill(id);
            return accountList;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="account">用户实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<int> Add(Role role)
        {
            var accountAdd = await _roleManager.Add(role);
            return 1;
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="account">修改DTO</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<int> Update(UpdateRoleDto role)
        {
            var accountUpdate = await _roleManager.Update(role);
            return 1;
        }
    }
}
